/**
 * Volces API 配置验证脚本
 * 使用方法: node test-volces-config.js
 */

import dotenv from 'dotenv';
import config from './src/config/index.js';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// 加载.env文件
dotenv.config({ path: resolve(__dirname, '.env') });

console.log('\n=== Volces API 配置检查 ===\n');

const checks = [
  {
    name: 'API Key',
    value: config.volces.apiKey,
    status: config.volces.apiKey ? '✓ 已配置' : '✗ 未配置',
    required: true,
  },
  {
    name: 'API URL',
    value: config.volces.apiUrl,
    status: config.volces.apiUrl ? '✓ 已配置' : '✗ 未配置',
    required: true,
  },
  {
    name: 'Model',
    value: config.volces.model,
    status: config.volces.model ? '✓ 已配置' : '✗ 未配置',
    required: true,
  },
  {
    name: 'Timeout',
    value: `${config.volces.timeout}ms`,
    status: '✓ 已配置',
    required: false,
  },
  {
    name: 'Max Retries',
    value: config.volces.maxRetries,
    status: '✓ 已配置',
    required: false,
  },
];

let allPassed = true;

checks.forEach((check) => {
  const statusSymbol = check.status.startsWith('✓') ? '✓' : '✗';
  const statusColor = statusSymbol === '✓' ? '\x1b[32m' : '\x1b[31m'; // Green or Red
  const resetColor = '\x1b[0m';

  console.log(`${statusColor}${statusSymbol}${resetColor} ${check.name}:`);
  console.log(`  值: ${check.value || '(未设置)'}`);
  console.log(`  状态: ${check.status}`);
  
  if (check.required && !check.value) {
    allPassed = false;
    console.log(`  ${statusColor}⚠️  这是必需的配置项！${resetColor}`);
  }
  
  console.log();
});

console.log('='.repeat(40));

if (allPassed) {
  console.log('\x1b[32m✓ 所有必需配置项已正确设置！\x1b[0m\n');
  console.log('下一步：运行 \x1b[36mnode test-ai-api.js\x1b[0m 测试API连接\n');
} else {
  console.log('\x1b[31m✗ 配置不完整，请检查.env文件\x1b[0m\n');
  console.log('请确保backend/.env文件包含以下配置：');
  console.log('\x1b[33m');
  console.log('VOLCES_API_KEY=your-api-key-here');
  console.log('VOLCES_API_URL=https://ark.cn-beijing.volces.com/api/v3/chat/completions');
  console.log('VOLCES_MODEL=deepseek-v3-1-terminus');
  console.log('\x1b[0m');
  process.exit(1);
}

